package com.wus.common.gen;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.wus.common.mybatis.core.domain.BaseEntity;
import org.springframework.util.StringUtils;

import java.util.Scanner;

/**
 * @Description: 实体类代码生成器
 * @Author: wus
 * @Date: 2024/12/25 11:58
 */
public class MyBatisPlusCodeGenerator {

    public static String scanner(String tip) {
        Scanner scanner = new Scanner(System.in);
        StringBuilder help = new StringBuilder();
        help.append("请输入" + tip + "：");
        System.out.println(help.toString());
        if (scanner.hasNext()) {
            String ipt = scanner.next();
            if (StringUtils.hasLength(ipt)) {
                return ipt;
            }
        }
        throw new MybatisPlusException("请输入正确的" + tip + "！");
    }


    public static void main(String[] args) {
        new AutoGenerator(new DataSourceConfig.Builder("jdbc:mysql://localhost:3306/song-cloud?useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Shanghai", "root", "123456").build())
                .global(new GlobalConfig.Builder()
                        .author("wus")
                        .outputDir(System.getProperty("user.dir") + "/code-generator/src/main/java")
                        .disableOpenDir()
                        .build())
                .template(new TemplateConfig.Builder()
                        .disable(TemplateType.CONTROLLER)
                        .build())
                .strategy(new StrategyConfig.Builder()
                        .enableCapitalMode()
                        .enableSkipView()
                        .addInclude(scanner("表名，多个英文逗号分割").split(","))
                        .entityBuilder()
                            .enableLombok()
                            .enableChainModel()
//                            .enableTableFieldAnnotation()
                            .enableRemoveIsPrefix()
                            .idType(IdType.ASSIGN_ID)
                            .naming(NamingStrategy.underline_to_camel)
                            .superClass(BaseEntity.class)
                            .addIgnoreColumns("create_dept", "create_by", "create_time", "update_by", "update_time", "del_flag")
                        .mapperBuilder()
                            .superClass(com.baomidou.mybatisplus.core.mapper.BaseMapper.class)
                        .build())
                .execute();
        ;
    }



}
